﻿using AutoMapper;
using GGGK.Film.IService;
using GGGK.Film.Models;
using GGGK.Film.Models.Dto;
using GGGK.Film.Models.Models;
using Microsoft.AspNetCore.Mvc;

namespace GGGK.Film.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class RoleMenuController : ControllerBase
    {
        private readonly IRoleMenuService svc;
        private readonly IMapper mapper;

        public RoleMenuController(IRoleMenuService svc,IMapper mapper)
        {
            this.svc = svc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("{roleId}")]
        public Result Get(int roleId)
        {
            var data = svc.Query(o => o.RoleId == roleId && o.Status == 1);

            return new Result() { code = 1, msg = "成功", data = data };
        }

        /// <summary>
        /// 授权
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("batch")]
        public Result Batch(RoleMenuBatchDto dto)
        {
            IEnumerable<Base_RoleMenu> list = dto.MenuIds.Select(o => new Base_RoleMenu()
            {
                MenuId = o,
                RoleId = dto.RoleId,
                CreateTime = DateTime.Now,
                Status = 1
            });

            if (svc.Add(dto.RoleId,list))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }
    }
}
